package com.uyayo.oms.system.biz;

import com.uyayo.oms.cms.model.TemplateFile;
import com.uyayo.oms.system.model.FileInfo;
import com.uyayo.oms.system.util.PathUtil;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by hfj on 2017/8/20.
 */

@Component
public class FileBiz {

    private final String UPLOAD_PATH = PathUtil.getRootClassPath()+ File.separator+"upload";

    public List<FileInfo> findAll(String fileDir) throws Exception {
            List<FileInfo> result = new ArrayList<>();
            File mainFile;
           if (StringUtils.isEmpty(fileDir)){
                mainFile = new File(UPLOAD_PATH+fileDir);
           }else{
                mainFile = new File(UPLOAD_PATH);
           }
            if(!mainFile.exists()||mainFile.listFiles().length<1) {
                throw new Exception("请检查模板目录是否存在！");
            }
            for(File file :mainFile.listFiles()){
                FileInfo fileInfo = new FileInfo();
                fileInfo.setFileName(file.getName());
                fileInfo.setFilePath(file.getAbsolutePath());
                fileInfo.setIsDirectory(file.isDirectory());
                result.add(fileInfo);
            }
            return result;
        }

        public TemplateFile findByPath(String path) throws Exception {
            File file = new File(path);
            if(!file.exists()) {
                throw new Exception("模板不存在请检查！");
            }
            TemplateFile templateFile = new TemplateFile();
            templateFile.setFileName(file.getName());
            templateFile.setFilePath(file.getAbsolutePath());
            templateFile.setContent(this.readTemplateFileContent(file));
            return templateFile;
        }


        public static void main(String[]args) throws Exception {
            List<FileInfo> list = new FileBiz().findAll(null);
            for (FileInfo fileInfo : list){
                System.out.println(fileInfo.getFileName()+"---------->"+ fileInfo.getFileName()+"["+ fileInfo.getFilePath()+"]");
            }
//      System.out.println(new TemplateFileServiceImpl().findByPath("E:\\work\\idea_app\\CMS\\CicadasCms\\CicadasCms\\target\\classes\\templates\\www\\blog\\about.html").getContent());
        }

        public  String readTemplateFileContent(File file) throws IOException {

                InputStream inputStream = new FileInputStream(file);
                byte[] buffer = new byte[1024];
                int len;
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                while((len = inputStream.read(buffer)) != -1) {
                    bos.write(buffer, 0, len);
                }
                bos.close();
                return new String(bos.toByteArray(), "utf-8");
        }

        @Async
        public  void writeTemplateFileContent(TemplateFile templateFile) throws IOException {
                OutputStream outputStream = new FileOutputStream(new File(templateFile.getFilePath()));
                OutputStreamWriter os = new OutputStreamWriter(outputStream, "utf-8");
                os.write(templateFile.getContent());
                os.flush();
                os.close();
        }


        public String delete() {
            return null;
        }
    }
